package com.mini.service;

import com.google.common.collect.Lists;
import com.mini.client.DictionariesBaseClient;
import com.mini.model.BusinessCity;
import com.mini.vo.BusinessChannelInfoVO;
import com.mini.vo.BusinessCityVO;
import com.mini.vo.BusinessItemInfoVO;
import com.mini.vo.BusinessResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zhaocg
 * @created: 2020/12/04 15:04
 */
@Service
public class DictionariesBaseService {

    @Autowired
    private DictionariesBaseClient dictionariesBaseClient;

    @Autowired
    private BusinessCityService businessCityService;



    /**
     * 获取项目名称
     *
     * @param token
     * @param itemId
     * @return
     */
    public String getItemName(String token ,Integer itemId){
        String itemName = "";
        List<BusinessItemInfoVO> itemInfoList = getItemInfo(token);
        if (!CollectionUtils.isEmpty(itemInfoList)) {
            Map<Integer,String> itemMap = itemInfoList.stream().collect(Collectors.toMap(BusinessItemInfoVO::getId, BusinessItemInfoVO::getItemName));
            itemName = itemMap.get(itemId);
        }
        return itemName;
    }

    /**
     * 获取项目map数据
     *
     * @param token
     * @return
     */
    public Map<Integer,String> getMapItemInfo(String token) {
        List<BusinessItemInfoVO> itemInfoList = getItemInfo(token);
        if (!CollectionUtils.isEmpty(itemInfoList)) {
            Map<Integer,String> itemMap = itemInfoList.stream().collect(Collectors.toMap(BusinessItemInfoVO::getId, BusinessItemInfoVO::getItemName));
            return itemMap;
        }
        return null;
    }

    /**
     * 获取项目列表
     *
     * @param token
     * @return
     */
    public List<BusinessItemInfoVO> getItemInfo(String token){
        BusinessResult<List<BusinessItemInfoVO>> result = dictionariesBaseClient.getItemInfo(token);
        if ( CollectionUtils.isEmpty(result.getData()) ) {
            return Lists.newArrayList();
        }
        return Lists.newArrayList(result.getData());
    }

    /**
     * 获取渠道名称
     *
     * @param channelCode
     * @return
     */
    public String getChannelName(String channelCode , String token){
        String channelName = "";
        List<BusinessChannelInfoVO> channelInfoList = getChannelInfoList(token);
        if (!CollectionUtils.isEmpty(channelInfoList)) {
            Map<String,String> itemMap = channelInfoList.stream().collect(Collectors.toMap(BusinessChannelInfoVO::getCode, BusinessChannelInfoVO::getName));
            channelName = itemMap.get(channelCode);
        }
        return channelName;
    }

    /**
     * 获取渠道列表
     *
     * @return
     */
    public List<BusinessChannelInfoVO> getChannelInfoList(String token){
        BusinessResult<List<BusinessChannelInfoVO>> result = dictionariesBaseClient.getChannelList(token);
        if ( CollectionUtils.isEmpty(result.getData()) ) {
            return Lists.newArrayList();
        }
        return Lists.newArrayList(result.getData());
    }

    /**
     * 获取渠道map数据
     *
     * @param token
     * @return
     */
    public Map<String,String> getMapChannelInfo(String token){
        List<BusinessChannelInfoVO> channelInfoList = getChannelInfoList(token);
        if (!CollectionUtils.isEmpty(channelInfoList)) {
            Map<String,String> itemMap = channelInfoList.stream().collect(Collectors.toMap(BusinessChannelInfoVO::getCode, BusinessChannelInfoVO::getName));
            return itemMap;
        }
        return null;
    }

    /**
     * 获取省份名称
     *
     * @param code
     * @return
     */
    public String getProvinceName(String code){
        String cityName = "";
        List<BusinessCity> channelInfoList = getProvinceList();
        if (!CollectionUtils.isEmpty(channelInfoList)) {
            Map<String,String> itemMap = channelInfoList.stream().collect(Collectors.toMap(BusinessCity::getCode, BusinessCity::getName));
            cityName = itemMap.get(code);
        }
        return cityName;
    }

    /**
     * 获取省份map数据
     * @return
     */
    public Map<String,String> getMapProvince(){
        List<BusinessCity> channelInfoList = getProvinceList();
        if (!CollectionUtils.isEmpty(channelInfoList)) {
            Map<String,String> itemMap = channelInfoList.stream().collect(Collectors.toMap(BusinessCity::getCode, BusinessCity::getName));
            return itemMap;
        }
        return null;
    }

    /**
     * 获取省份列表
     *
     * @return
     */
    public List<BusinessCity> getProvinceList() {
        List<BusinessCity> cityList = businessCityService.getProvinceList();

        if ( CollectionUtils.isEmpty(cityList) ) {
            return Lists.newArrayList();
        }
        return cityList;
    }

}
